
//
//  UIBezierPath_public extension.swift
//  boleh
//
//  Created by BO ZHOU on 2020/9/8.
//  Copyright © 2020 BO ZHOU. All rights reserved.
//

import UIKit


public extension UIBezierPath {
    
    // 填充, 并设置填充色
    func fill(with color: UIColor) {
        color.set()
        self.fill()
    }
    
    /**
     * 画线, 并设置线条颜色, 宽度
     *
     * - parameter color: 线条颜色
     * - parameter lineWidth: 线条宽度
     * - parameter lineCapStyle: 端点类型
     * - parameter lineJoinStyle: 线条连接类型
     */
    func stroke(with color: UIColor, lineWidth: CGFloat = 1, lineCapStyle: CGLineCap = .butt, lineJoinStyle: CGLineJoin = .miter) {
        color.set()
        self.lineWidth = lineWidth
        self.stroke()
    }
    
    // 两点之间画线
    func setLine(from startPoint: CGPoint, to endPoint: CGPoint) {
        if self.currentPoint != startPoint {
            self.move(to: startPoint)
        }
        self.addLine(to: endPoint)
    }
    
    // 多个点之间连线
    func setLines(with linePoints: [CGPoint]) {
        
        if linePoints.count < 2 {
            return
        }
        
        let startPoint = linePoints[0]
        if self.currentPoint != startPoint {
            self.move(to: startPoint)
        }
        
        let subLinePoints = linePoints.suffix(from: 1)
        for point in subLinePoints {
            self.addLine(to: point)
        }
    }
    
    // 两点之间画线
    func addLine(from startPoint: CGPoint, to endPoint: CGPoint) {
        self.setLine(from: startPoint, to: endPoint)
    }
    
    // 多个点之间连线
    func addLines(with linePoints: [CGPoint]) {
        
        self.setLines(with: linePoints)
    }
}
